<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%String ctx = request.getContextPath();%>
<title>create question discuss</title>
</head>

<script>

	$(document).ready(function(){
		<c:if test="${fn:length(type) gt 0}">
		$("#title").html("编辑简答题")
		$("#discusscontent").html("${question.content}")
		</c:if>
	});


	function addQuestionDiscuss() 
	{
		let content = $("#discusscontent").val();
		if(content.length === 0) {
			alert('请输入题干');
			return;
		}
		
		$.ajax(
			{
				type:"POST",
				url: "<%=ctx%>/question?act=addQuestion&type=discuss",
				data: {
					"content" :content,
				},
				success: function(data, status) {
					alert(data);
					AjaxLoad('<%=ctx%>/control?act=createDiscuss')
				},
			  	error: function(req, status, msg) {
					alert(msg)
			  	}
			}
		)
	}

	function updateQuestionDiscuss()
	{
		let content = $("#discusscontent").val();
		if(content.length === 0) {
			alert('请输入题干');
			return;
		}

		$.ajax(
			{
				type:"POST",
				url: "<%=ctx%>/question?act=updateQuestion&type=discuss&id="+"${question.id}",
				data: {
					"content" :content,
				},
				success: function(data, status) {
					alert(data);
					AjaxLoad('<%=ctx%>/control?act=questionOpera')
				},
			  	error: function(req, status, msg) {
					alert(msg)
			  	}
			}
		)
	}

</script>

<body>
	<div id="list" class="col-md-10 col-xs-12">
		<h3>创建简答题</h3>
		<hr>
    
		<div class="fiter col-md-10 col-xs-12">
   			<textarea class="form-control" id="discusscontent" rows="6" placeholder="请输入题干"></textarea>
			<h5>&nbsp;</h5>

			<c:choose>
				<c:when test="${fn:length(type) gt 0}">
					<button type="button" class="btn btn-success" onclick="updateQuestionDiscuss()">确定</button>
				</c:when>
				<c:otherwise>
					<button type="button" class="btn btn-success" onclick="addQuestionDiscuss()">确定</button>
				</c:otherwise>
			</c:choose>
		</div>
	</div>
</body>
</html>